package
{

	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.AntiAliasType;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	class SpriteButton extends Sprite 
	{
	    	private var _rollOver:Loader;
	   	 	private var _rollOut:Loader;
	   	 	private var _label:TextField;
	   	 	
	   	 	private var _name:String;
	   	 	private var _labelStr:String;
	
		    public function SpriteButton( nameStr:String, labelStr ) 
		    {
		        _name = nameStr;
		     	buttonMode = true; 
		     	
		     	_labelStr = labelStr;
		     	
		     	
		     	
		     	createButton();
		    }
		
		   	    
		    private function createHitArea( myName:String ):Sprite
			{
				var hitAreaSprite:Sprite = new Sprite();
				
				hitAreaSprite.graphics.beginFill(0x000000,1);
				hitAreaSprite.graphics.drawRect(10, 5, 150, 40);
				hitAreaSprite.graphics.endFill();
				
				hitAreaSprite.alpha = 0;
				hitAreaSprite.name = myName + "HitAreaSprite";
				hitAreaSprite.buttonMode = true;
				hitAreaSprite.mouseEnabled = true;
				
				hitAreaSprite.addEventListener(MouseEvent.CLICK, buttonClickHandler, false, 0, true);
				hitAreaSprite.addEventListener(MouseEvent.ROLL_OVER, buttonRollOverHandler, false, 0, true);
				hitAreaSprite.addEventListener(MouseEvent.ROLL_OUT, buttonRollOutHandler, false, 0, true);
				
				return hitAreaSprite;
			}
			
			private function createButton():void
			{
							
				_rollOver = new Loader();
				_rollOut = new Loader();
				
				Loader(_rollOver).load( new URLRequest("images/buttonRollOver.png") );
				Loader(_rollOut).load( new URLRequest("images/buttonRollOut.png") );
				
				_rollOver.visible = false;
				
				_rollOut.mouseEnabled = false;
				_rollOver.mouseEnabled = false;
				
				_label = new TextField();
				_label.text = _labelStr;
				_label.autoSize = TextFieldAutoSize.CENTER;
				_label.antiAliasType = AntiAliasType.ADVANCED;
				
				_label.y = 15;
				
				addChild( _rollOver );
				addChild( _rollOut );
				addChild( _label );
				addChild(createHitArea(_name));
			}
			
			private function buttonRollOutHandler(e:MouseEvent):void
			{
				_rollOver.visible = false;
				_rollOut.visible = true;
			}
			
			private function buttonRollOverHandler(e:MouseEvent):void
			{
				_rollOver.visible = true;
				_rollOut.visible = false;
			}
			
			private function buttonClickHandler(e:MouseEvent):void
			{
				
			}
			
		}
}